<?php 
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Account extends MY_Controller {
    /**
    * Responsable for auto load the model
    * @return void
    */
   public function __construct(){
		parent::__construct();
		if(!$this->session->userdata('userdata')){
			$this->session->set_flashdata('return', $this->uri->uri_string);
			redirect('/dang-nhap');		
		}
		$this->load->model('users_m', 'users');						
		$this->load->library('form_validation');		
   }
	
	function index(){
		$data = array();
		$type = $this->input->post('type');
		if($type){
			switch ($type){
				case 'hoso':
					$tmp = $this->hoso();
					$data = array_merge($data, $tmp);				
				break;
				case 'dichvu':
					$tmp = $this->dang_ky_dich_vu();
					$data = array_merge($data, $tmp);
				break;
				case 'album':
					$tmp = $this->cap_nhat_album();
					$data = array_merge($data, $tmp);
				break;
				case 'password':
					$tmp = $this->cap_nhat_password();
					$data = array_merge($data, $tmp);
				break;
			}
		}
		$data['user'] 		= $this->session->userdata('userdata');
		$this->load->model('tuyendung_m', 'tuyendung');		
		if($data['user']['loai'] == 1){
			// echo '<pre>';print_r($data['user']);die;					
			$this->load->model('canhan_m', 'canhan');
			$this->load->model('register_m', 'register');
			$this->load->model('dichvu_m', 'dichvu');
			$this->load->model('album_m', 'album');	
			$data['hoso'] 		= $this->canhan->get_current_canhan();
			$data['point']		=	$this->tuyendung->get_point();
			$data['tinh_thanh']	=	$this->register->get_tinh_thanh();
			foreach($this->register->get_quan_huyen($data['hoso']->tinh_thanh_id) as $obj)
				$data['quan_huyen'][$obj->id]	= $obj->ten;
			$dichvu				= $this->dichvu->get_dichvu();
			$tmp = array();
			foreach($dichvu as $key => $obj){
				$tmp[$obj->id]	=	$obj;
			}
			$dichvu = $tmp;
			$data['dichvu_dk']= $this->dichvu->get_dichvu_dangky();		
			foreach($data['dichvu_dk'] as $obj)			
				unset($dichvu[$obj->id]);		
			$data['dichvu_cdk']	= $dichvu;
			//echo '<pre>';print_r($data['dichvu_cdk']);die;
			$data['album']		= $this->album->get_album();
		}else if($data['user']['loai'] == 2){			
			$data['hoso'] 		= 	$this->tuyendung->get_current_profile();
			// echo '<pre>';print_r($data['hoso']);die;
			$data['point']		=	$this->tuyendung->get_point();
			$data['hispay']	=	$this->tuyendung->get_history_payment($data['user']['id']);
		}
		$data['content'] 	= 'account/taikhoan';
		//$data['quangcao'] = 'tpl/quangcao';
		$this->load->view('tpl/template', $data);		
	}
	
	function hoso(){
		$data = array();
		$post = $this->input->post();
		// echo '<pre>';print_r($post);die;
		if($post){		
			$user	= $this->session->userdata('userdata');
			if($user['loai'] == 1){
				$this->form_validation->set_error_delimiters('<div>', '</div>');			
				$this->form_validation->set_rules('ho_ten', 'Họ tên', 'trim|required|min_length[4]');
				$this->form_validation->set_rules('ngay_sinh', 'Ngày sinh', 'required|exact_length[10]');
				$this->form_validation->set_rules('gioi_tinh','required');
				$this->form_validation->set_rules('hon_nhan','required');
				$this->form_validation->set_rules('tinh_thanh','required');
				//$this->form_validation->set_rules('avatar','trim');			
				$this->form_validation->set_rules('di_dong1', 'Di động 1', 'trim|required|min_length[10]|max_length[11]');
				$this->form_validation->set_rules('di_dong2', 'Di động 2', 'trim');
				$this->form_validation->set_rules('can_nang', 'Cân nặng', 'required|min_length[2]|max_length[3]');
				$this->form_validation->set_rules('chieu_cao_m', 'Chiều cao', 'greater_than[0]|required|max_length[1]');			
				$this->form_validation->set_rules('chieu_cao_cm', 'Chiều cao(cm)', 'greater_than[10]|required|max_length[2]');			
				$this->form_validation->set_rules('vong_1', 'Vòng 1', 'greater_than[50]|required|max_length[3]');			
				$this->form_validation->set_rules('vong_2', 'Vòng 2', 'greater_than[50]|required|max_length[3]');			
				$this->form_validation->set_rules('vong_3', 'Vòng 3', 'greater_than[50]|required|max_length[3]');			
				$this->form_validation->set_rules('yahoo','trim');
				$this->form_validation->set_rules('facebook','trim');
				$this->form_validation->set_rules('hoc_van','trim');
				$this->form_validation->set_rules('ngoai_ngu','trim');
				$this->form_validation->set_rules('nang_khieu','trim');
				$this->form_validation->set_rules('kinh_nghiem','trim');
				$this->form_validation->set_rules('tgian_tu','trim');			
				$this->form_validation->set_rules('tgian_den','trim');			
				if ($this->form_validation->run() == FALSE){
					$data['error'] = validation_errors();				
				}else{
					$user_id = $user['id'];
					if($user_id){
						//insert canhan table
						$canhan['ho_ten']			= $post['ho_ten'];
						$canhan['ngay_sinh']		= date("Y-m-d", strtotime(str_replace("/","-",$post['ngay_sinh'])));					
						$canhan['gioi_tinh']		= $post['gioi_tinh'];
						$canhan['hon_nhan']		= $post['hon_nhan'];
						$canhan['tinh_thanh_id']= $post['tinh_thanh'];
						$canhan['quan_huyen_id']= $post['quan_huyen'];
						$canhan['di_dong1']		= $post['di_dong1'];
						//$canhan['di_dong2']		= $post['di_dong2'];
						$canhan['can_nang']		= $post['can_nang'];
						$canhan['chieu_cao_m']	= $post['chieu_cao_m'];
						$canhan['chieu_cao_cm']	= $post['chieu_cao_cm'];
						$canhan['vong1']			= $post['vong_1'];
						$canhan['vong2']			= $post['vong_2'];
						$canhan['vong3']			= $post['vong_3'];
						$canhan['yahoo']			= $post['yahoo'];
						$canhan['facebook']		= $post['facebook'];
						$canhan['hoc_van']		= $post['hoc_van'];
						$canhan['ngoai_ngu']		= $post['ngoai_ngu'];
						$canhan['nang_khieu']	= $post['nang_khieu'];
						$canhan['kinh_nghiem']	= $post['kinh_nghiem'];
						$canhan['tgian_tu']		= date('H:i:s', strtotime($post['tgian_tu']));
						$canhan['tgian_den']		= date('H:i:s', strtotime($post['tgian_den']));
						date_default_timezone_set('Asia/Bangkok');
						$canhan['ngay_cap_nhat']= date('Y-m-d H:i:s');
						$canhan['trang_thai']	= 0;
						$updated = $this->users->update_canhan($canhan, $user_id);
						if($updated){
							// $data['message'] 	= "Cập nhật thành công.";
							$this->session->set_flashdata('message', 'Cập nhật thành công.');
						}else{
							// $data['danger'] 	= "Cập nhật không thành công.";
							$this->session->set_flashdata('error', 'Cập nhật không thành công.');
						}
						redirect('/tai-khoan');
					}else{
						redirect('/dang-nhap');
					}
				}
			}else if($user['loai'] == 2){				
				$obj['ten']				= $post['ten'];
				$obj['gioi_thieu']	= $post['gioi_thieu'];
				$obj['nguoi_lien_he']= $post['nguoi_lien_he'];
				$obj['dia_chi']		= $post['dia_chi'];
				$obj['dien_thoai']	= $post['dien_thoai'];
				$obj['di_dong']		= $post['di_dong'];
				date_default_timezone_set('Asia/Bangkok');
				$obj['ngay_cap_nhat']= date('Y-m-d H:i:s');
				$updated = $this->users->update_nhatuyendung($obj, $user['id']);
				if($updated){
					$data['message'] 	= "Cập nhật thành công.";
				}else{
					$data['danger'] 	= "Cập nhật không thành công.";
				}
			}
		}
		return $data;
	}
	
	function dang_ky_dich_vu(){
		$data = array();
		$json = $this->input->post('dichvu_json');		
		$objs = json_decode($json);
		//echo $json;die;
		if($objs){			
			$this->load->model('dichvu_m', 'dichvu');
			$dichvu = array();
			$userdata = $this->session->userdata('userdata');			
			foreach($objs as $obj)
				$dichvu[]	= array(
					'canhan_id' => $userdata['canhan_id'],
					'dichvu_id'	=> $obj->id,
					'catse'		=> $obj->catse
				);
			$updated = $this->dichvu->dang_ky_dich_vu($dichvu);
			if($updated)
				$data['message'] 	= "Đăng ký dịch vụ thành công.";
			else
				$data['danger'] 	= "Đăng ký dịch vụ không thành công.";
		}
		return $data;
	}
	
	function cap_nhat_album(){
		$data = array();
		$thumb = $this->input->post('thumb');		
		$this->load->model('album_m', 'album');
		if(!$thumb) $thumb = array('0'=>0);
			$album = array();
			foreach($thumb as $key => $value)
				$album[]	= array(
					'id'			=> $key,
					'thu_tu'		=> $value
				);
			//print('<pre>');print_r($album);die;
			$updated = $this->album->update_album($album);
			//if($updated)
			$data['message'] 	= "Cập nhật Album ảnh thành công.";
			//else $data['danger'] 	= "Cập nhật Album ảnh không thành công.";
		//}
		return $data;
	}
	
	function cap_nhat_password(){
		$data = array();
		$post = $this->input->post();
		//echo '<pre>';print_r($post);die;
		if($post){
			$user	= $this->session->userdata('userdata');
			$this->form_validation->set_rules('password', 'Mật khẩu', 'required|min_length[6]');
			$this->form_validation->set_rules('repassword', 'Nhập lại mật khẩu', 'required|matches[password]');
			if ($this->form_validation->run() == FALSE)
				$data['error'] = validation_errors();
			else{
				$user = array();
				$password	=	md5($post['password']);
				$updated = $this->users->change_password($password);
				if($updated)
					$data['message'] ="Đổi mật khẩu thành công.";
				else
					$data['message'] ="Đổi mật khẩu không thành công.";
			}
		}
		return $data;
	}
}